import instacne from "../request";
import type { Ref } from "vue";
import qs from "qs";

/**
 * 获取商品属性列表数据
 */
interface IProductAtrrListParams {
  pageNum?: number | Ref<number>;
  pageSize?: number | Ref<number>;
}

interface IProductAtrrListItem {
  attributeCount: number;
  id: number;
  name: string;
  paramCount: number;
}

interface IProductAtrrListRes {
  list: IProductAtrrListItem[];
  pageNum: number;
  pageSize: number;
  total: number;
  totalPage: number;
}

interface IProductAtrrAdd {
  filterType: number;
  handAddStatus: number;
  inputList: string;
  inputType: number;
  name: string;
  productAttributeCategoryId: number;
  relatedStatus: number;
  searchType: number;
  selectType: number;
  sort: number;
  type: number;
}
const getProductCategoryList = (
  data?: IProductAtrrListParams
): Promise<IProductAtrrListRes> => {
  return instacne.get("/productAttribute/category/list", {
    params: data,
  });
};

/**
 * 添加商品属性
 */
const addProductAtrr = (
  data: IProductAtrrListItem
): Promise<IProductAtrrListItem> => {
  return instacne.post("/productAttribute/category/create", data);
};
/*
 * 修改商品属性分类
 * /productAttribute/category/delete/{id}
 */
const updataProductAtrr = (data: IProductAtrrListItem) => {
  return instacne.post(`/productAttribute/category/delete/${data.id}`, data);
};
/*
 * 删除属性数据
 */
// 删除属性列表
const deleteProductAtrr = (data: number): Promise<IProductAtrrAdd> => {
  return instacne.get(`/productAttribute/category/delete/${data}`);
};

// productAttribute/category/list
// 分页获取所有商品属性分类
interface productAttrParams {
  pageNum?: number | Ref<number>;
  pageSize?: number | Ref<number>;
  cid?: number | Ref<number>;
  type?: number | Ref<number>;
}

interface productAttrItem {
  attributeCount?: number;
  id: number;
  name: string;
  paramCount?: number;
  filterType: number;
  handAddStatus: number;
  inputList: string;
  inputType: number;
  productAttributeCategoryId: number;
  relatedStatus: number;
  searchType: number;
  selectType: number;
  sort: number;
  type: number;
  // 商品类型
  productType: string;
}

interface productAttrRes {
  list: productAttrItem[];
  pageNum: number;
  pageSize: number;
  total: number;
  totalPage: number;
}

const getproductAttrList = (
  data?: productAttrParams
): Promise<productAttrRes> => {
  return instacne.get("/productAttribute/category/list", {
    params: data,
  });
};

// /productAttribute/category/create
// 添加商品属性分类
interface addproductAttrParams {
  name?: string | Ref<string>;
}
const addproductAttr = (data: addproductAttrParams) => {
  return instacne.post("/productAttribute/category/create", qs.stringify(data));
};

// /productAttribute/category/update/{id}
// 修改商品属性分类
interface upproductAttrParams {
  name?: string | Ref<string>;
  id?: number | Ref<number>;
}
const upproductAttr = (data: upproductAttrParams) => {
  return instacne.post(
    `/productAttribute/category/update/${data.id}`,
    qs.stringify(data)
  );
};
// /productAttribute/category/delete/{id}
// 删除单个商品属性分类
const deleteproductAttr = (id: number) => {
  return instacne.get(`/productAttribute/category/delete/${id}`);
};

/**
 * /productAttribute/list/{cid}
根据分类查询属性列表或参数列表
 * */
const getclassifyproductAttributelist = (
  data: productAttrParams
): Promise<productAttrRes> => {
  return instacne.get(`/productAttribute/list/${data.cid}`, {
    params: data,
  });
};

// /productAttribute/update/{id}
// 修改商品属性信息
const productAttributeupdate = (data: productAttrItem) => {
  return instacne.post(`/productAttribute/update/${data.id}`, data);
};
// /productAttribute/create
// 添加商品属性信息
const productAttributeadddata = (data: productAttrItem) => {
  return instacne.post("/productAttribute/create", data);
};

// /productAttribute/delete
// 批量删除商品属性
const productAttributedelete = (aaa: string) => {
  return instacne.post("/productAttribute/delete", qs.stringify({ ids: aaa }));
};
export {
  getProductCategoryList,
  addProductAtrr,
  updataProductAtrr,
  deleteProductAtrr,
  productAttributedelete,
  productAttributeadddata,
  productAttributeupdate,
  getclassifyproductAttributelist,
  getproductAttrList,
  addproductAttr,
  deleteproductAttr,
};
export type {
  IProductAtrrListItem,
  productAttrItem,
  productAttrRes,
  productAttrParams,
};
